/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 15-11-9
 * Time: 下午2:19
 * To change this template use File | Settings | File Templates.
 */

(function ($) {
    function renderDialog() {
        if (typeof($("#messageDialog")[0]) != 'undefined') {
            $("#messageDialog").remove();
        }
        var dialogTitle = "消息";
        var htmlStr = "" +
            "<div class='modal fade' id='messageDialog' tabindex='-1' role='dialog' style='z-index: 20000'>                                                               " +
            "    <div class='modal-dialog'>                                                                                                                 " +
            "        <div class='modal-content'>                                                                                                                     " +
            "            <div class='modal-header'>                                                                                                                  " +
            "                <button type='button' class='close' data-dismiss='modal'>&times;</button>                                                               " +
            "                <h4 class='modal-title' id='dinging_lable'>" + dialogTitle + "</h4>                                                                                " +
            "            </div>                                                                                                                                      " +
            "            <div class='modal-body' id='msgBody' style='margin: 10px;'>                                                                                                                    " +
            "            </div>                                                                                                                                      " +
            "           <div class='modal-footer'>" +
            "               <button type='button' class='btn btn-default' data-dismiss='modal'>关闭</button>" +
            "           </div>" +
            "        </div>                                                                                                                                          " +
            "    </div>                                                                                                                                              " +
            "</div>                                                                                                                                                  ";

        var dialog = $(htmlStr);
        $("body").append(htmlStr);
    }

    function addWaiting(){
        //先判断是否存在
        var waiting=$("#sw-straining")[0];
        //console.log(typeof(waiting));
        if(typeof(waiting) == "undefined"){
            var htmlStr="<div class='sw-straining' id='sw-straining'>"+
            "<div class='load-container load'>"+
            "    <div class='loader'>Loading...</div>"+
            "   </div>"+
            "</div>";
            $("body").append(htmlStr);
        }
    }
    function showWaiting(){
        var waiting=$("#sw-straining")[0];
        //console.log(typeof(waiting));
        if(typeof(waiting) == "undefined"){
            addWaiting();
        }
        waiting=$("#sw-straining");
        waiting.css("display","block");
    }
    function hideWaiting(){
        var waiting=$("#sw-straining");
        if(typeof(waiting) == "undefined"){
            return;
        }
        waiting.css("display","none");
    }

    $.doSubmit = function (options) {
//        var defaults={
//            foreground:'red',
//            background:'yellow'
//        };
//        var opts = $.extend(defaults,options);
        var submitUrl = options.submitUrl;
        var data = options.data;
        var dataType = "text";
        var callBack = options.callBack;
        var webContext = options.webContext;
        var traditional = options.traditional;
        var showwait = true;
        if (typeof (options.showwait) != "undefined") {
            showwait = options.showwait;
        }
        if (typeof(options.traditional) == "undefined") {
            traditional = false;
        }
        if (webContext.lastIndexOf("/") != webContext.length - 1) {
            webContext += "/";
        }

        //console.log("webContext->" + webContext);
        var type = options.type;
        if (typeof(type) == "undefined") {
            type = "POST";
        }
        var async = options.async;
        if (typeof(async) == "undefined") {
            async = true;
        }
        //引入等待css
        if (showwait) {
            showWaiting();
        }
        renderDialog();
        //创建一个dialog
        //调用ajax
        $.ajax({
            type: type,
            url: submitUrl,
            data: data,
            dataType: dataType,
            async:async,
            traditional:traditional,
            success: function (response) {
//                console.log(typeof(response)+"--dd");
                if (response.indexOf("\<!DOCTYPE html\>") > 0) {//弹出登录界面
                    //var bodyBegin = response.indexOf("<body>") + 6;
                    //var bodyEnd = response.indexOf("</body>");
                    //var length = bodyEnd - bodyBegin;
                    var loginUrl = webContext + 'auth/login';
                    var body = "<div style='text-align: center;'>" +
                        "登录过期，<a href='" + loginUrl + "'>重新登录</a>" +
                        "</div>";
                    $("#msgBody").html(body);
                    $("#messageDialog").modal("show");
                } else {
                    //var redata=$.parseJSON(response);
                    //alert("response->"+response);
                    //try {
                        var redata = response;
                        callBack(redata);
                    //} catch (err) {
                    //    console.error(err.message)
                    //}
                }
            },
            complete: function (e, xhr, settings) {
                if(showwait) {
                    hideWaiting();
                }
                //alert(e.status);
                if (e.status === 200) {

                } else if (e.status === 304) {

                } else if (e.status === 403) {//弹出无权限窗口
                    var defaultUrl = webContext + 'auth/default';
                    var loginUrl = webContext + 'auth/login';
                    var body = "<div style='text-align: center;'>" +
                        "没有权限，<a href='" + defaultUrl + "?from=index'>返回默认页</a>，<a href='" + loginUrl + "'>重新登录</a>" +
                        "</div>";
                    $("#msgBody").html(body);
                    $("#messageDialog").modal("show");
                } else if (e.status === 500) {
                    var indexUrl = webContext + 'index.jsp';
                    var body = "<div style='text-align: center;'>" +
                        "服务器出了点小问题，<a href='" + indexUrl + "'>返回主页</a>" +
                        "</div>";
                    $("#msgBody").html(body);
                    $("#messageDialog").modal("show");
                }
            }
        });
    }
})(jQuery);